import api from './config'
// 角色创建请求接口
export interface CharacterCreateRequest {
  name: string
  appearance: string
  background: string
  personality: string
  classicLines: string
  // userId由后端自动处理
  // image通过单独的上传接口处理
}

export interface CharacterCreateResponse {
  code: number
  msg: string | null
  data: number | null // 后端直接返回角色ID数字，或者null
}

export interface UploadAvatarResponse {
  code: number
  msg: string | null
  data: string // 头像URL
}

// 角色信息接口
export interface Character {
  id: number
  name: string
  image?: string | null
  isPublic?: boolean | null
  appearance: string
  background: string
  personality: string
  classicLines: string
  userId?: number | null
  createTime?: string | null
  updateTime?: string | null
  isDeleted?: number | null
}

// 角色列表响应接口
export interface CharacterListResponse {
  code: number
  msg: string | null
  data: Character[]
}

// 角色详情响应接口
export interface CharacterDetailResponse {
  code: number
  msg: string | null
  data: Character | null
}

export const characterApi = {
  // 创建角色
  create: async (data: CharacterCreateRequest): Promise<CharacterCreateResponse> => {
    return api.post('/character/add', data)
  },

  // 上传角色头像
  uploadAvatar: async (file: File, characterId: number): Promise<UploadAvatarResponse> => {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('characterId', characterId.toString())
    
    // 不要手动设置Content-Type，让浏览器自动设置multipart/form-data和boundary
    return api.post('/user/common/uploadCharacterAvatar', formData)
  },

  // 角色广场：获取公开的角色列表
  getSquareList: async (): Promise<CharacterListResponse> => {
    return api.get('/character/square')
  },

  // 获取角色列表（旧）
  getList: async (): Promise<CharacterListResponse> => {
    return api.get('/character/List')
  },

  // 获取角色详情
  getDetail: async (id: number): Promise<CharacterDetailResponse> => {
    return api.get(`/character/${id}`)
  },

  // 获取用户的角色列表
  getUserCharacters: async (): Promise<CharacterListResponse> => {
    return api.get('/character/my-list')
  },

  // 查询角色关注状态
  getFollowStatus: async (id: number): Promise<{ code: number; msg: string | null; data: boolean }> => {
    return api.get(`/user/follow/status?id=${id}`)
  },

  // 关注角色
  followCharacter: async (id: number): Promise<{ code: number; msg: string | null; data: any }> => {
    return api.post('/user/follow/follow', null, { params: { id } })
  },

  // 取消关注角色，和关注发送的同一个url，不用修改
  unfollowCharacter: async (id: number): Promise<{ code: number; msg: string | null; data: any }> => {
    return api.post('/user/follow/follow', null, { params: { id } })
  },

  // 获取我的关注列表
  getFollowList: async (): Promise<CharacterListResponse> => {
    return api.get('/user/follow/list')
  }
}